package com.zte.cl.handler;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.zte.cl.model.News;
import com.zte.cl.utils.StringUtil;

/**
 * 新闻过滤处理类--关键词黑名单过滤
 * @author lh
 *
 */
public class NewsBlackKeywordHandler extends NewsHandler {
	
	private static final Log log = LogFactory.getLog(NewsBlackKeywordHandler.class);
	
	// 关键词黑名单
	private String[] blackKeyWords;
	
	public NewsBlackKeywordHandler(String[] blackKeyWords) {
		this.blackKeyWords = blackKeyWords;
	}

	@Override
	public List<News> filterNews(List<News> list) {
		List<News> filteredNewsList = new ArrayList<News>();
		if(blackKeyWords.length > 0) {
			for(int i = 0; i < list.size(); i++) {
				News news = list.get(i);
				String keyword = StringUtil.processNullStr(news.getKeyword());
				String title = StringUtil.processNullStr(news.getTitle());
				boolean black = false;
				
				for(int j = 0; j < blackKeyWords.length; j++) {
					if(keyword.contains(blackKeyWords[j]) || title.contains(blackKeyWords[j])) {
						black = true;
						log.info("包含黑名单关键词blackKeyword:" + blackKeyWords[j] + ", news[{keyword:" + keyword + ", title:" + title + "}]");
						break;
					}
				}
				
				if(!black) {
					filteredNewsList.add(news);
				}
			}
		} else {
			log.info("关键词黑名单为空!");
			filteredNewsList = list;
		}
		
		if(getNextHandler() != null) {
			return getNextHandler().filterNews(filteredNewsList);
		} else {
			return filteredNewsList;
		}
	}

}
